package pref.junx.flybird.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import pref.junx.flybird.R;
import pref.junx.flybird.activity.event_info.Event_Info_Actvity;
import pref.junx.flybird.entity.Event;
import pref.junx.flybird.tools.TimeChange;
import pref.junx.flybird.util.ExpandableViewHoldersUtil;

/**
 * Created by JunX on 17/5/13.
 */

public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> {

    private List<Event> data;
    private Context context;
    private ExpandableViewHoldersUtil.OpenItem keepOne = new ExpandableViewHoldersUtil.OpenItem<SimpleViewHolder>();

    public SimpleAdapter(Context context, List<Event> data) {

        this.context = context;
        this.data = data;

    }

    @Override
    public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_simple, parent, false);
        SimpleViewHolder holder = new SimpleViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(SimpleViewHolder holder, int position) {
        holder.bind(position);
        holder.value.setText(data.get(position).getEvent_value());
        holder.zone.setText(data.get(position).getEvent_zone());
        holder.table.setText(data.get(position).getEvent_table());
        if(data.get(position).getEvent_type()!=null){
            holder.group.setText("@"+data.get(position).getEvent_type());
        }else {
            holder.group.setText("");
        }
        holder.time.setText(TimeChange.stampToDate(data.get(position).getEvent_time()));

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class SimpleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, ExpandableViewHoldersUtil.Expandable, ExpandableViewHoldersUtil.ExpandableTop {


        LinearLayout submit_view;

        RelativeLayout top_view;

        TextView value;

        TextView zone;

        TextView table;

        TextView group;

        TextView time;

        ImageView more;

        public SimpleViewHolder(View view) {
            super(view);
            view.setOnClickListener(this);
            submit_view=(LinearLayout)view.findViewById(R.id.submit_view);
            top_view=(RelativeLayout)view.findViewById(R.id.top_view);
            table=(TextView) view.findViewById(R.id.item_author);
            value = (TextView) view.findViewById(R.id.itme_value);
            more = (ImageView) view.findViewById(R.id.more);
            zone = (TextView) view.findViewById(R.id.item_content);
            group = (TextView) view.findViewById(R.id.item_author_aite);
            time = (TextView) view.findViewById(R.id.item_publish_time);
            more.setOnClickListener(this);
        }

        public View getSubmitView() {
            return submit_view;
        }

        public View getTopView() {
            return top_view;
        }

        @Override
        public void onClick(View v) {
            keepOne.toggle(this);
            switch (v.getId()){
                case R.id.more:
                     Event e=data.get(getAdapterPosition());
                    Intent intent = new Intent();
                    intent.putExtra("title", data.get(getAdapterPosition()).getEvent_table());
                    intent.putExtra("zone", data.get(getAdapterPosition()).getEvent_zone());
                    intent.putExtra("time", data.get(getAdapterPosition()).getEvent_time());
                    intent.putExtra("context", data.get(getAdapterPosition()).getEvent_value());
                    intent.putExtra("id", data.get(getAdapterPosition()).getEvent_id());
                    intent.setClass(context, Event_Info_Actvity.class);
                    context.startActivity(intent);
                    break;
            }
        }

        public void bind(int pos) {
            keepOne.bind(this, pos);

        }

        @Override
        public View getExpandView() {
            return submit_view;
        }

        @Override
        public View getExpandTopView() {
            return top_view;
        }
    }

}
